/**
 * Copyright (c)  HeiFeiDeFengZheng.Co.Ltd. All rights reserved.
 **/
package com.dexin.warehousedemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dexin.warehousedemo.common.Result;
import com.dexin.warehousedemo.entity.User;
import com.dexin.warehousedemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 功能描述：
 *
 * @author huifeidefengzheng
 * @since 2024-01-10
 *
 **/
@RestController
@RequestMapping("/te")
public class HelloController {

    @Resource
    private UserService userService;


    @GetMapping("/hello")
    public String hello() {
        return "hello sss";
    }
    //新增
    @PostMapping("/save")
    public Result save(@RequestBody User user) {
        boolean save = userService.save(user);
        Result.suc(save);
        return Result.suc(save);
    }
    // 更新
    @PostMapping("/updateById")
    public Result updateById(@RequestBody User user) {

        return Result.suc(userService.updateById(user));
    }
    // 删除
    @PostMapping("/removeById")
    public Result removeById(Integer id) {
        return Result.suc(userService.removeById(id));
    }

    @GetMapping("/users")
    public Result list() {
        List<User> list = userService.list();
        return Result.suc(list,Long.valueOf(list.size()));
    }

    @GetMapping("/users/{pageNo}/{pageSize}")
    public Result listPage(@PathVariable("pageNo") long pageNo, @PathVariable("pageSize") long pageSize, @RequestParam(required = false) String name) {
        Page<User> userPage = new Page<>();
        userPage.setCurrent(pageNo);
        userPage.setSize(pageSize);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (null != name) {
            wrapper.like("name", name);
        }
        Page<Map<String, Object>> mapPage = userService.pageMaps(new Page<>(pageNo, pageSize), wrapper);

        return Result.suc(mapPage.getRecords(),mapPage.getTotal(),mapPage.getSize());
    }

    // 查询模糊
    @PostMapping("/like")
    public List<User> like(@RequestBody User user) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(User::getName,user.getName());
        return userService.list(wrapper);
    }


}
